package com.ys.service.hr.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ys.entity.hr.TwlStock;
import com.ys.entity.hr.TwlStockChange;
import com.ys.entity.hr.TwlStockFrozenRunning;
import com.ys.entity.hr.TwlStockRunning;
import com.ys.entity.hr.TwlStockTransfer;
import com.ys.enume.hr.TwlStockChangeEnum;
import com.ys.mapper.hr.HrMapper;
import com.ys.mapper.hr.TwlStockChangeMapper;
import com.ys.mapper.hr.TwlStockFrozenRunningMapper;
import com.ys.mapper.hr.TwlStockMapper;
import com.ys.mapper.hr.TwlStockRunningMapper;
import com.ys.service.hr.TwlStockService;

@Service("twlStockService")
public class TwlStockServiceImpl implements TwlStockService {
	@Autowired
	private HrMapper hrMapper;
	@Autowired
	private TwlStockMapper twlStockMapper;
	@Autowired
	private TwlStockChangeMapper twlStockChangeMapper;
	@Autowired
	private TwlStockRunningMapper twlStockRunningMapper;
	@Autowired
	private TwlStockFrozenRunningMapper twlStockFrozenRunningMapper;
	
	/**
	 * 冻结资金，同时添加流水
	 */
	@Override
	public void change(Long stockId, Integer type, Long sourceId, String sourceBid, Integer sourceNum) throws Exception {
		if (type == TwlStockChangeEnum.Type.IN.value) {
			
			// 添加物料库存变动记录
			TwlStockChange twlStockChange = null;
			{
				String remark = null;
				twlStockChange = TwlStockChange.twlStockChange(type, sourceId, sourceBid, sourceNum, remark, 1);
				twlStockChangeMapper.insert(twlStockChange);
			}
			
			// 添加物料库存流水
			TwlStockRunning twlStockRunning = null;
			{
				Long stockChangeId = twlStockChange.getId();
				Integer changeNum = sourceNum;
				Integer numLeft = null;
				twlStockRunning = TwlStockRunning.twlStockRunning(stockChangeId, stockId, changeNum, numLeft, 1);
				twlStockRunningMapper.insert(twlStockRunning);
			}
		} else if (type == TwlStockChangeEnum.Type.OUT.value) {
			// 减少库存
			hrMapper.updateTwlStockOfNum(stockId, sourceNum * -1);

			
			// 添加物料库存变动记录
			TwlStockChange twlStockChange = null;
			{
				String remark = null;
				twlStockChange = TwlStockChange.twlStockChange(type, sourceId, sourceBid, sourceNum, remark, 1);
				twlStockChangeMapper.insert(twlStockChange);
			}
			
			// 添加物料库存流水
			TwlStockRunning twlStockRunning = null;
			{
				Long stockChangeId = twlStockChange.getId();
				Integer changeNum = sourceNum;
				Integer numLeft = null;
				twlStockRunning = TwlStockRunning.twlStockRunning(stockChangeId, stockId, changeNum, numLeft, 1);
				twlStockRunningMapper.insert(twlStockRunning);
			}
		} else if (type == TwlStockChangeEnum.Type.TRANSFER.value) {
			
			/*
			 * 1、原库存扣减
			 * 2、添加库存变动记录
			 * 3、添加转出方物料库存流水
			 * 4、添加新归属库存
			 * 5、添加转入方物料库存流水
			 */
			
			// 减少原归属库存
			hrMapper.updateTwlStockOfNum(stockId, sourceNum * -1);
			
			// 添加物料库存变动记录
			TwlStockChange twlStockChange = null;
			{
				String remark = null;
				twlStockChange = TwlStockChange.twlStockChange(type, sourceId, sourceBid, sourceNum, remark, 1);
				twlStockChangeMapper.insert(twlStockChange);
			}
			
			// 添加转出方物料库存流水
			TwlStockRunning twlStockRunningFrom = null;
			{
				Long stockChangeId = twlStockChange.getId();
				Integer changeNum = sourceNum * -1;
				Integer numLeft = null;
				twlStockRunningFrom = TwlStockRunning.twlStockRunning(stockChangeId, stockId, changeNum, numLeft, 1);
				hrMapper.insertTwlStockRunning(twlStockRunningFrom);
			}
			
			// 添加新归属库存
			TwlStockTransfer entity = new TwlStockTransfer().setBid(sourceBid).queryFirst();
			TwlStock stockOld = new TwlStock().setId(stockId).queryFirst();
			TwlStock stock = null;
			{
				String companyVid = stockOld.getCompanyVid();
				String orgid = entity.getOrgidTo();
				String materialCode = stockOld.getMaterialCode();
				Integer num = sourceNum;
				Integer numAll = sourceNum;
				Integer numFrozen = 0;
				BigDecimal price = stockOld.getPrice();
				Long stockInDetailId = stockOld.getStockInDetailId();
				String stockInDay = stockOld.getStockInDay();
				String remark = stockOld.getRemark();
				stock = TwlStock.twlStock(companyVid, orgid, materialCode, num, numAll, numFrozen, price,
						stockInDetailId, stockInDay, remark, 1);
				twlStockMapper.insert(stock);
			}
			
			// 添加转入方物料库存流水
			TwlStockRunning twlStockRunningTo = null;
			{
				Long stockChangeId = twlStockChange.getId();
				Long stockId2 = stock.getId();
				Integer changeNum = sourceNum;
				Integer numLeft = null;
				twlStockRunningTo = TwlStockRunning.twlStockRunning(stockChangeId, stockId2, changeNum, numLeft, 1);
				hrMapper.insertTwlStockRunning(twlStockRunningTo);
			}
			
		}
	}

	@Override
	public void frozen(Long stockId, Integer type, String sourceBid, Long sourceId, Integer sourceNum) throws Exception {
		
		// 可用数量减少，冻结数量增加
		hrMapper.updateTwlStockWhenFrozen(stockId, sourceNum);
		
		// 添加冻结流水
		TwlStockFrozenRunning twlStockFrozenRunning = null;
		{
			Integer changeNum = sourceNum;
			Integer numLeft = 0;
			twlStockFrozenRunning = TwlStockFrozenRunning.twlStockFrozenRunning(stockId, type, sourceBid, sourceId, changeNum, numLeft, 1);
			twlStockFrozenRunningMapper.insert(twlStockFrozenRunning);
		}
	}

	@Override
	public void unfrozen(Long stockId, Integer type, String sourceBid, Long sourceId, Integer unfrozenNum) throws Exception {
		
		// 可用数量增加，冻结数量减少
		hrMapper.updateTwlStockWhenFrozen(stockId, unfrozenNum * -1);
		
		// 添加冻结流水
		TwlStockFrozenRunning twlStockFrozenRunning = null;
		{
			Integer changeNum = unfrozenNum * -1;
			Integer numLeft = 0;
			twlStockFrozenRunning = TwlStockFrozenRunning.twlStockFrozenRunning(stockId, type, sourceBid, sourceId, changeNum, numLeft, 1);
			twlStockFrozenRunningMapper.insert(twlStockFrozenRunning);
		}
	}
}
